package org.jash.learnmvp.news;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.MaterialToolbar;

import org.jash.learnmvp.R;
import org.jash.learnmvp.entity.NewsDetailEntity;
import org.jash.learnmvp.mvp.BaseActivity;

public class NewsDetailActivity extends BaseActivity<ConStruct.INewsDetailView, NewsDetailPresenter> implements ConStruct.INewsDetailView {
    private int id;
    private MaterialToolbar toolbar;
    private ImageView image;
    private TextView content;
    private CommentFragment fragment;
    @Override
    protected int layoutId() {
        return R.layout.activity_news_detail;
    }

    @Override
    protected void initData() {
        id = getIntent().getIntExtra("id", 0);
        fragment = CommentFragment.newInstance(id);
        presenter = new NewsDetailPresenter();
        presenter.loadNewsDetail(id);
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        image = findViewById(R.id.img);
        content = findViewById(R.id.content);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        findViewById(R.id.comment).setOnClickListener(v -> {
            fragment.show(getSupportFragmentManager(), null);
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void newsDetailSuccess(NewsDetailEntity.DataBean bean) {
        toolbar.setTitle(bean.getTitle());
        content.setText(bean.getContent());
        Glide.with(image)
                .load(bean.getImgurl())
                .into(image);
    }

    @Override
    public void failure(Throwable t) {
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}